package com.china_goldcard.igas.database.dao;

import com.china_goldcard.igas.database.core.GreenDaoManager;
import com.china_goldcard.igas.database.model.User;

import java.util.List;

/**
 * Created by jk on 16/10/11.
 */

public class UserDaoImpl {


    private static UserDaoImpl instance;

    /**
     * 获取实体类
     * @return
     */
    public static UserDaoImpl getInstance(){
        if (instance == null) {
            synchronized (UserDaoImpl.class) {
                if (instance == null) {
                    instance = new UserDaoImpl();
                }
            }
        }
        return instance;
    }

    /**
     * 查询所有的用户
     * @return
     */
    public List<User> queryAll(){
        return GreenDaoManager.getInstance().getmDaoSession().getUserDao().queryBuilder().list();
    }

    /**
     * 获取总记录数
     * @return
     */
    public long count(){
        return GreenDaoManager.getInstance().getmDaoSession().getUserDao().queryBuilder().count();
    }

    /**
     * 保存用户
     * @param user
     * @return
     */
    public boolean saveUser(User user){
        return GreenDaoManager.getInstance().getmDaoSession().getUserDao().insert(user) > 0;
    }

    /**
     * 批量保存用户
     * @param users
     * @return
     */
    public void saveUserByBatch(List<User> users){
        GreenDaoManager.getInstance().getmDaoSession().getUserDao().insertInTx(users);
    }

}
